package com.shop.loan.proxy;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.loan.mapper.*;
import com.shop.loan.model.entity.ChannelByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.enums.OrderStatus;
import com.shop.loan.model.view.ChannelStatistics;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.ChannelService;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChannelStatisticsProxy implements DataProxy<ChannelStatistics> {

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    Util util;

    @Resource
    ChannelByMybatisMapper channelByMybatisMapper;

    @Resource
    RedisUtil redisUtil;

    @Resource
    ChannelService channelService;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.clear();
        return null;
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        list.clear();
        List<Long> channelIds = new ArrayList<>();
        String channelStr = util.getSearchParam("channel", 1);
        if (StringUtils.hasText(channelStr)) {
            Long channelId = Long.valueOf(channelStr);
            channelIds.add(channelId);
        } else {
            channelIds = channelService.getChannelIds();
        }

        String pageIndexStr = util.getSearchParam("pageIndex", 1);
        String pageSizeStr = util.getSearchParam("pageSize", 1);
        Integer pageIndex = pageIndexStr == null ? 1 : Integer.parseInt(pageIndexStr);
        Integer pageSize = pageSizeStr == null ? 10 : Integer.parseInt(pageSizeStr);

        String beginTime = util.getSearchParam("searchDay", 0);
        String endTime = util.getSearchParam("searchDay", 1);

        ChannelByMybatis channelByMybatis = new ChannelByMybatis();
        channelByMybatis.setIds(channelIds);
        PageHelper.startPage(pageIndex, pageSize);
        Page<ChannelByMybatis> channelByMybatisPage = channelByMybatisMapper.selectByList(channelByMybatis);
        for (ChannelByMybatis channel : channelByMybatisPage) {
            Map itemMap = new HashMap();
            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setStartTime(beginTime);
            loanOrderByMybatis.setEndTime(endTime);
            loanOrderByMybatis.setChannelId(channel.getId());
            loanOrderByMybatis.setChannelStatistics(true);

            //注册数量
            Integer registerNum = userByMybatisMapper.selectCountByChannel(channel.getId(), beginTime, endTime);
            //下单数量
            Integer orderTotalNum = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
            //待审核人数
            loanOrderByMybatis.setStatus(OrderStatus.wait_auth.getStatus());
            Integer waitAuthOrderNum = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
            //待还款
            loanOrderByMybatis.setStatus(OrderStatus.underway.getStatus());
            Integer orderUnderwayNum = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
            //已结束
            loanOrderByMybatis.setStatus(OrderStatus.end.getStatus());
            Integer orderEndNum = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);

            //待放款
            loanOrderByMybatis.setStatus(OrderStatus.wait_loan.getStatus());
            Integer orderWaitLoanNum = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);


            orderUnderwayNum = orderUnderwayNum == null ? 0 : orderUnderwayNum;
            orderEndNum = orderEndNum == null ? 0 : orderEndNum;
            Integer orderSuccessNum = orderUnderwayNum + orderEndNum;

            Long pv = 0L, uv = 0L;
            if (StringUtils.hasLength(beginTime) && StringUtils.hasLength(endTime)) {
                Date beginTimeDate = DateUtil.parseDate(beginTime);
                while (DateUtil.compare(beginTimeDate, DateUtil.parseDate(endTime)) <= 0) {
                    String pvStr = redisUtil.get(RedisCache.channelPageOpenPv + channel.getCode() + "_" + DateUtil.formatDate(beginTimeDate));
                    pvStr = pvStr == null ? "0" : pvStr;
                    Long uvLong = redisUtil.hlen(RedisCache.channelPageOpenUv + channel.getCode() + "_" + DateUtil.formatDate(beginTimeDate));
                    uvLong = uvLong == null ? 0 : uvLong;

                    pv = pv + Long.parseLong(pvStr);
                    uv = uv + uvLong;
                    beginTimeDate = DateUtil.offsetDay(beginTimeDate, 1);
                }
            }

            itemMap.put("id", channel.getId());
            itemMap.put("name", channel.getName());
            itemMap.put("pv", pv);
            itemMap.put("uv", uv);
            itemMap.put("registerNum", registerNum == null ? 0 : registerNum);
            itemMap.put("orderTotalNum", orderTotalNum == null ? 0 : orderTotalNum);
            itemMap.put("waitAuthOrderNum", waitAuthOrderNum == null ? 0 : waitAuthOrderNum);
            itemMap.put("orderWaitLoanNum", orderWaitLoanNum == null ? 0 : orderWaitLoanNum);
            itemMap.put("orderSuccessNum", orderSuccessNum);
            list.add(itemMap);
        }
    }


}
